package org.example.controller;

import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.example.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestController {

    @Autowired
    private ProcessRuntime processRuntime;


    @Autowired
    private TaskRuntime taskRuntime;


    @Autowired
    private SecurityUtil securityUtil;


    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @RequestMapping(value = "/start/{id}")
    public void start(@PathVariable String id) {
        //启动一个流程
        ProcessInstance holiday = runtimeService.startProcessInstanceByKey("holiday", id);
    }


    @RequestMapping(value = "/task/list/{name}")
    public void listTask(@PathVariable String name) {
        //ACT_RU_TASK
        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey("holiday").taskAssignee(name).list();
        for (Task task : tasks) {
            System.out.println(task);
        }
    }


    @RequestMapping(value = "/task/complete/{id}")
    public void completeTask(@PathVariable String id) {
        taskService.complete(id);
    }


    @RequestMapping(value = "/instance/suspend/{id}")
    public void suspendInstance(@PathVariable String id) {
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionId(id).singleResult();
        if (!processInstance.isSuspended()) {
            runtimeService.suspendProcessInstanceById(processInstance.getId());
        }
    }
}
